
$(document).ready(function() {
	setupLocalStorage();
	if (getValue("action") == 'csv')
	{
		showResultsCSV();
	} else if (getValue("action") == 'clear') {
		localStorage["trialsBackup"] = localStorage["trials"];
		localStorage["trials"] = JSON.stringify([]);
	} else if (getValue("action") == "restore") {
		localStorage["trials"] = localStorage["trialsBackup"];
	} else {
		showResults();
	}
});

// DATA STORE

	function setupLocalStorage()
	{
		if (!localStorage["trials"]) {
		  localStorage["trials"] = JSON.stringify([]);
		}
	}
	
	function showResults()
	{
		var trials = JSON.parse(localStorage["trials"]);
      	$.each(trials, function(i, trial) {
			var trialStartTime = new Date(trial.trialStartTime);
			var trialEndTime = new Date(trial.trialEndTime);
			var trialPreBlankTime = new Date(trial.trialPreBlankTime);
			var trialPreTargetTime = new Date(trial.trialPreTargetTime);
			var trialPreMaskTime = new Date(trial.trialPreMaskTime);
			var trialDuration = trialEndTime.getTime() - trialStartTime.getTime();
			var primeDuration = trialPreBlankTime.getTime() - trialStartTime.getTime();
			var blankDuration = trialPreTargetTime.getTime() - trialPreBlankTime.getTime();
			var targetDuration = trialPreMaskTime.getTime() - trialPreTargetTime.getTime();
			var inputCollectedTime = new Date(trial.inputCollectedTime);
			var inputDelay = inputCollectedTime.getTime() - trialEndTime.getTime();
			var prime = parseInt(trial.primeImageID);
			var primeString = null;
			if (prime<25) {
				primeString = "pos"+prime+".bmp";	
			} else {
				primeString = "neg"+(prime-24)+".bmp";	
			}
			var neutralString = "pic"+trial.neutralImageID+".bmp";
			$("#AMP_cell").append("<p>Trial "+(i+1)+"<br/>");
			$("#AMP_cell").append("&nbsp;&nbsp;Subject Id: "+trial.subjectId+"<br/>");
			$("#AMP_cell").append("&nbsp;&nbsp;Prime Image: "+primeString+"<br/>");
			$("#AMP_cell").append("&nbsp;&nbsp;Neutral Image: "+neutralString+"<br/>");
			$("#AMP_cell").append("&nbsp;&nbsp;Trial # in Set: "+trial.trialsCompletedInSet+"<br/>");
			$("#AMP_cell").append("&nbsp;&nbsp;Trial Start: "+trialStartTime.toString()+"<br/>");
			$("#AMP_cell").append("&nbsp;&nbsp;Trial Duration (Milliseconds): "+trialDuration+"<br/>");
			$("#AMP_cell").append("&nbsp;&nbsp;Prime Duration (Milliseconds): "+primeDuration+"<br/>");
			$("#AMP_cell").append("&nbsp;&nbsp;Blank Duration (Milliseconds): "+blankDuration+"<br/>");
			$("#AMP_cell").append("&nbsp;&nbsp;Target Duration (Milliseconds): "+targetDuration+"<br/>");
			$("#AMP_cell").append("&nbsp;&nbsp;Input Delay (Milliseconds): "+inputDelay+"<br/>");
			$("#AMP_cell").append("&nbsp;&nbsp;Result: "+trial.result+"<br/>");
			$("#AMP_cell").append("</p>");
		});
		// var trialsString = JSON.stringify(trials);
		// $("#AMP_cell").html(trialsString);
	}
	
	function showResultsCSV()
	{
		$("#AMP_cell").append("Trial #,");
			$("#AMP_cell").append("Subject Id,");
			$("#AMP_cell").append("Prime Image,");
			$("#AMP_cell").append("Neutral Image,");
			$("#AMP_cell").append("Trial # in Set,");
			$("#AMP_cell").append("Trial Start,");
			$("#AMP_cell").append("Trial Duration (Milliseconds),");
			$("#AMP_cell").append("Prime Duration (Milliseconds),");
			$("#AMP_cell").append("Blank Duration (Milliseconds),");
			$("#AMP_cell").append("Target Duration (Milliseconds),");
			$("#AMP_cell").append("Input Delay (Milliseconds),");
			$("#AMP_cell").append("Result");
			$("#AMP_cell").append("<br/>");
		var trials = JSON.parse(localStorage["trials"]);
      	$.each(trials, function(i, trial) {
			var trialStartTime = new Date(trial.trialStartTime);
			var trialEndTime = new Date(trial.trialEndTime);
			var trialPreBlankTime = new Date(trial.trialPreBlankTime);
			var trialPreTargetTime = new Date(trial.trialPreTargetTime);
			var trialPreMaskTime = new Date(trial.trialPreMaskTime);
			var trialDuration = trialEndTime.getTime() - trialStartTime.getTime();
			var primeDuration = trialPreBlankTime.getTime() - trialStartTime.getTime();
			var blankDuration = trialPreTargetTime.getTime() - trialPreBlankTime.getTime();
			var targetDuration = trialPreMaskTime.getTime() - trialPreTargetTime.getTime();
			var inputCollectedTime = new Date(trial.inputCollectedTime);
			var inputDelay = inputCollectedTime.getTime() - trialEndTime.getTime();
			var prime = parseInt(trial.primeImageID);
			var primeString = null;
			if (prime<25) {
				primeString = "pos"+prime+".bmp";	
			} else {
				primeString = "neg"+(prime-24)+".bmp";	
			}
			var neutralString = "pic"+trial.neutralImageID+".bmp";
			$("#AMP_cell").append((i+1)+",");
			$("#AMP_cell").append(trial.subjectId+",");
			$("#AMP_cell").append(primeString+",");
			$("#AMP_cell").append(neutralString+",");
			$("#AMP_cell").append(trial.trialsCompletedInSet+",");
			$("#AMP_cell").append(trialStartTime.toString()+",");
			$("#AMP_cell").append(trialDuration+",");
			$("#AMP_cell").append(primeDuration+",");
			$("#AMP_cell").append(blankDuration+",");
			$("#AMP_cell").append(targetDuration+",");
			$("#AMP_cell").append(inputDelay+",");
			$("#AMP_cell").append(trial.result+"");
			$("#AMP_cell").append("<br/>");
		});
		// var trialsString = JSON.stringify(trials);
		// $("#AMP_cell").html(trialsString);
	}
	
	function getValue(varname)
	{
	  var url = window.location.href;
	  var qparts = url.split("?");
	  if (qparts.length == 0)
	  {
		return "";
	  }
	  var query = qparts[1];
	  if (query == null) return "";
	  var vars = query.split("&");
	  var value = "";
	  for (i=0;i<vars.length;i++)
	  {
		var parts = vars[i].split("=");
		if (parts[0] == varname)
		{
		  value = parts[1];
		  break;
		}
	  }
	  value = unescape(value);
	  value.replace(/\+/g," ");
	  return value;
	}